package webmarket.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import webmarket.common.DataTablesParamUtility;
import webmarket.common.DatatableResponse;
import webmarket.common.MarkerController;
import webmarket.common.ShopcategoryController;
import webmarket.controller.transferobjects.JQueryDataTableParamModel;
import webmarket.controller.transferobjects.LogTO;
import webmarket.controller.transferobjects.LogTOFull;
import webmarket.controller.transferobjects.MerchandiseTO;
import webmarket.controller.transferobjects.UserTO;
import webmarket.jpacontrollers.LogJpaController;
import webmarket.jpacontrollers.ShopJpaController;
import webmarket.jpacontrollers.UserJpaController;
import webmarket.model.Merchandise;
import webmarket.model.ReadLog;
import webmarket.model.User;

@Controller
@RequestMapping("/administration")
public class AdministrationController extends ControllerBase {

	@RequestMapping("/users")
	public ModelAndView administration_users(ModelMap model) {
		return new ModelAndView("administration_users", model);
	}

	@RequestMapping(value = "/users/json", method = RequestMethod.GET)
	public @ResponseBody
	DatatableResponse<UserTO> administration_users_post(HttpServletRequest request) {
		JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request);
		logger.debug(param.toString());
		DatatableResponse<UserTO> response = new DatatableResponse<UserTO>(param);
		String userName = param.sSearch;
		String sortCol = param.getSortColumnName();
		String sortDir = param.sSortDirection;
		int limit = param.iDisplayLength;
		int offset = param.iDisplayStart;
		UserJpaController ujc = new UserJpaController();
		List<User> data = ujc.getUsers(userName, sortCol, sortDir, limit, offset);
		response.setData(UserTO.convert(data), ujc.getCount(), ujc.getCount(userName));
		logger.debug(response.toString());
		return response;
	}

	@RequestMapping(value = "/users/setActive", method = RequestMethod.POST)
	public @ResponseBody
	Boolean administration_users_setactive_post(int id, boolean active) {
		UserJpaController ujc = new UserJpaController();
		logger.debug("administration_users_setactive_post:" + id + ":" + active);
		return ujc.setActive(id, active);
	}

	@RequestMapping("/logs")
	public ModelAndView administration_logs(ModelMap model) {
		model.put("markers", MarkerController.getAllMarkerOptions());
		return new ModelAndView("administration_logs", model);
	}

	@RequestMapping(value = "/logs/json", method = RequestMethod.GET)
	public @ResponseBody
	DatatableResponse<LogTO> administration_logs_post(HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "", value = "markerName") String markerName) {
		logger.entry();
		JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request);
		param.sSearch = markerName;
		logger.debug(param.toString());
		DatatableResponse<LogTO> response = new DatatableResponse<LogTO>(param);
		String sortCol = param.getSortColumnName();
		String sortDir = param.sSortDirection;
		int limit = param.iDisplayLength;
		int offset = param.iDisplayStart;
		LogJpaController ljc = new LogJpaController();
		List<ReadLog> data = ljc.getLogs(markerName, sortCol, sortDir, limit, offset);
		response.setData(LogTO.convert(data), ljc.getCount(), ljc.getCount(markerName));
		logger.debug(response.toString());
		logger.exit();
		return response;
	}

	@RequestMapping(value = "/logs/details")
	public @ResponseBody
	LogTOFull administration_logs_logDetails(@RequestParam(value = "id") int id) {
		logger.entry(id);
		ReadLog log = new LogJpaController().find(id);
		LogTOFull result = new LogTOFull(log);
		logger.debug(result);
		logger.exit();
		return result;
	}

	@RequestMapping(value = "/logs/log")
	public @ResponseBody
	ReadLog administration_logs_getLog(@RequestParam(required = true, value = "id") int id) {
		ReadLog readLog = new LogJpaController().find(id);
		return readLog;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/sales")
	public ModelAndView admin_merch(ModelMap model) {
		model.put("categoryOptions", ShopcategoryController.getForSelect());
		return new ModelAndView("administration_sales", model);
	}
	
	@RequestMapping(value = "/sales/getSales")
	public @ResponseBody
	DatatableResponse<MerchandiseTO> admin_merch(HttpServletRequest request) {
		JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request);
		logger.debug(param.toString());
		DatatableResponse<MerchandiseTO> response = new DatatableResponse<MerchandiseTO>(param);
		String sortCol = param.getSortColumnName();
		String sortDir = param.sSortDirection;
		int limit = param.iDisplayLength;
		int offset = param.iDisplayStart;
		String categoryIdString = request.getParameter("categoryId");
		Integer categoryId = null;
		if (!StringUtils.isBlank(categoryIdString) && !categoryIdString.equals("0")) {
			categoryId = Integer.parseInt(categoryIdString);
		}
		ShopJpaController sjc = new ShopJpaController();
		List<Merchandise> merches = sjc.getMerchandises(categoryId, null, null, null, null, sortCol, sortDir, limit, offset);
		response.setData(MerchandiseTO.convert(merches), sjc.getCount(), sjc.getCount(categoryId, null, null, null, null));
		logger.debug(response.toString());
		logger.exit();
		return response;
	}
}
